﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace QLKS.Models
{
    static public class LoaiPhongModel
    {
        static public DataTable GetDmLoaiPhong()
        {
            string query = "SELECT * FROM LoaiPhong";
            return DataProviderModel.QueryResults(query).Tables[0];
        }

        //Lấy danh sách loại phòng
        static public DataTable GetDmLoaiPhong(string selectColumns)
        {
            return DataProviderModel.QueryResults(string.Format("select {0} from LoaiPhong", selectColumns.Trim() == "" ? "*" : selectColumns)).Tables[0];
        }

        //Tạo mới phòng
        static public DataTable CreateNewLoaiPhong()
        {
            DataTable tb = DataProviderModel.QueryResults("select * from LoaiPhong WHERE 1=0").Tables[0];
            DataRow row = tb.NewRow();
            row["malp"] = -1;
            row["tenlp"] = "";
            row["giatheogio"] = 0;
            row["giatheongay"] = 0;
            tb.Rows.Add(row);
            return tb;
        }

        //Create QueryUpdate
        static public string CreateQueryUpdate(string columns)
        {
            string[] listColumns = columns.Split(',');
            string result = string.Empty;
            foreach (string cl in listColumns)
                result += cl + "= @" + cl + ",";
            if (result.Length > 0)
                result = result.Substring(0, result.Length - 1);
            return result;
        }

        //Get Identity
        public static int GetIdentity()
        {
            return (int)DataProviderModel.ScalarExecute("select Max(malp) from LoaiPhong");
        }

        //Thêm loại phòng
        static public int ThemLoaiPhong(DataTable tb)
        {
            int id = 0;
            string malp = tb.Rows[0]["malp"].ToString();
            if (malp == "-1") // Thêm mới
            {
                DataProviderModel.Insert(tb, "LoaiPhong", "malp");
                id = GetIdentity();
            }
            else
            {
                DataProviderModel.Update(tb, "LoaiPhong", "malp");
            }
            return id;
        }

        //Xóa Loại phòng
        static public int XoaLoaiPhong(int ma)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("malp");
            tb.Rows.Add(ma);
            return DataProviderModel.Detele(tb, "LoaiPhong");
        } 
    }
}
